{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上下文管理协议"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Sample:\n",
    "    def __enter__(self):\n",
    "        print(\"Entering the with block\")\n",
    "        return self\n",
    "    def __exit__(self, exc_type, exc_val, exc_tb):\n",
    "        # 释放资源\n",
    "        print(\"Exiting the with block\")\n",
    "    def do_something(self):\n",
    "        print(\"Doing something in the with block\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Entering the with block\n",
      "<__main__.Sample object at 0x7d33d85ecbe0>\n",
      "Doing something in the with block\n",
      "Exiting the with block\n"
     ]
    }
   ],
   "source": [
    "with Sample() as sp:\n",
    "    print(sp)\n",
    "    sp.do_something()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "简化版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "open file:  test.txt\n",
      "{'success': True}\n",
      "close file:  test.txt\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[3], line 15\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m file_process(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtest.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m     14\u001b[0m     \u001b[38;5;28mprint\u001b[39m(f)\n\u001b[0;32m---> 15\u001b[0m     \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n\u001b[1;32m     16\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mprocess file\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "import contextlib\n",
    "\n",
    "\n",
    "@contextlib.contextmanager\n",
    "def file_process(file_path):\n",
    "    try:\n",
    "        print(\"open file: \", file_path)\n",
    "        yield {'success': True}\n",
    "    finally:\n",
    "        print(\"close file: \", file_path)\n",
    "\n",
    "\n",
    "with file_process(\"test.txt\") as f:\n",
    "    print(f)\n",
    "    1/ 0\n",
    "    print(\"process file\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "jupter-env-10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
